'''
Created on 14/03/2011
@summary: MPRIS2 utils
@author: chukysoria
'''

MPRIS2_DBUS_OBJECT = '/org/mpris/MediaPlayer2'
MPRIS2_DBUS_GENERIC = 'org.mpris.MediaPlayer2'
MPRIS2_DBUS_PLAYER = 'org.mpris.MediaPlayer2.Player'
MPRIS2_DBUS_PLAYLISTS = 'org.mpris.MediaPlayer2.Playlists'

    
class Mpris(object):
    '''
    classdocs
    '''
       
    def get_mpris2_buses(self):  
        '''
        @summary: Lists the available players running.
        @return: List of the MPRIS2 players' DBUS
        '''
        import re
        import dbus
        
        org_mpris_re = re.compile('^org\.mpris\.MediaPlayer2\.([^.]+)$')
          
        bus = dbus.SessionBus()
        return [ name for name in bus.list_names() if org_mpris_re.match(name) ]
        
    def get_friendly_name(self, mpris2_bus):
        '''
        @summary: Return the friendly name for an MPRIS2 interface.
        @param mpris2_bus: Dbus of the player (string)
        @return: Friendly name of the player (string)
        '''
        from util.utils import DBusInterface
        prop = DBusInterface(mpris2_bus, MPRIS2_DBUS_OBJECT, MPRIS2_DBUS_GENERIC)
        return prop.get('Identity')
